package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.MbomRsBomItem;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.MbomRsBomItemDao;
import com.efast.dmom.mes.datahandlers.base.PartRelateDataHandler;
import com.efast.dmom.mes.entity.MbomRsBomItemEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;

@Component
public class MbomRsBomItemDataHandler extends PartRelateDataHandler<MbomRsBomItem>{
	@Autowired
	private MbomRsBomItemDao mbomRsBomItemDao;
	
	@Override
	public ResultBean save(MbomRsBomItem obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomRsBomItemEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.setBaseEntityData(entity);
		int save = mbomRsBomItemDao.save(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(MbomRsBomItem obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomRsBomItemEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.updateBaseEntityData(entity);
		int save = mbomRsBomItemDao.update(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(MbomRsBomItem obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomRsBomItemEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.updateBaseEntityData(entity);
		int save = mbomRsBomItemDao.delete(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean saveBatch(List<MbomRsBomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<MbomRsBomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<MbomRsBomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MbomRsBomItem getById(long id) {
		MbomRsBomItemEntity entity = mbomRsBomItemDao.getById(id);
		if(entity!=null) {
			MbomRsBomItem mbomRsBomItem = new MbomRsBomItem();
			mbomRsBomItem.setInnerData(entity);
			return mbomRsBomItem;
		}
		return null;
	}
	
	
	public List<MbomRsBomItem> getByMbomId(long mbomRsBomId){
		List<MbomRsBomItemEntity> byMbomRsBomId = mbomRsBomItemDao.getByMbomRsBomId(mbomRsBomId);
		
		if(byMbomRsBomId!=null && byMbomRsBomId.size()>0) {
			List<MbomRsBomItem> list = new ArrayList<MbomRsBomItem>();
			for(MbomRsBomItemEntity entity:byMbomRsBomId) {
				MbomRsBomItem mbomRsBomItem = new MbomRsBomItem();
				mbomRsBomItem.setInnerData(entity);
				list.add(mbomRsBomItem);
			}
			return list;
		}
		return null;
	}
}
